#!/usr/bin/bash

path=`cd "$( dirname ${BASH_SOURCE[0]} )" && pwd`
cd $path
source config.sh
#检测系统类型
source $bin/get_platform.sh


tool=$1
version=$2
method=$3
export=$4
clear=$5

if [[ $tool == '' || $version == '' || $method == '' || $export == '' ]];then
	echo ==================================
	echo 请选择需要安装的开发工具
	echo 1.Python
	echo 2.Nodejs
	echo 3.Java
	echo ==================================
	read -p "请选择：" tool
	if [[ $tool = 1 ]];then
		tool=python
	elif [[ $tool = 2 ]];then
		tool=node
	elif [[ $tool = 3 ]];then
		tool=java
	else
		echo "没有这个选项。"
		exit
	fi


	#选择版本
	echo ==================================
	echo 请选择需要安装的版本
	source $bin/recommend.sh $tool
	echo ==================================
	read -p "请选择：" version
	version=`sed -n "$version""p" $config/$tool/recommend/"$dev_env_system"_${dev_env_version%%"."*}_default.txt`
	version=${version#*" "}
	if [[ $version = "" ]];then
		echo "没有这个选项。"
		exit
	fi


	echo ==================================
	echo 请选择需要安装的方式
	echo python全版本暂无官方编译版本。请选择其他安装方式谢谢。
	echo nodejs全版本暂无自编译版本。请选择其他安装方式谢谢。
	echo 1.自动编译安装
	echo 2.官方编译版本
	#echo 3.系统源版本
	echo ==================================
	read -p "请选择：" method
	
	#配置环境变量
	echo ==================================
	echo 请选择调用环境的方式
	echo 1.通过shell脚本调用
	echo 2.配置环境变量
	echo ==================================
	read -p "请选择：" export
fi


if [ -d $system_env/$tool$version ];then
	echo "检测到您已经安装了$tool$version，是否清理重新配置？y or n:"
	if [[ $clear == '' ]];then
		read -p "请选择：" clear
	fi
	if [[ $clear == 'y' || $clear == 'Y' ]];then
		echo "y"
		echo "开始清理。。。"
		source uninstall.sh $tool $version $method
	fi
fi


if [[ $tool = 'python' && $method = 2 ]];then
	echo python全版本暂无官方编译版本。请选择其他安装方式谢谢。
	exit
elif [[ $tool = 'node' && $method = 1 ]];then
	echo nodejs全版本暂无自编译版本。请选择其他安装方式谢谢。
	exit
elif [ ! -d $system_env/$tool$version ];then
	#开始安装
	if [[ $method = 1 ]];then
		source $bin/$tool/$dev_env_system/$version/compile.sh
	elif [[ $method = 2 ]];then
		source $bin/$tool/$dev_env_system/$version/install.sh
	else
		echo "没有这个选项。"
		exit
	fi
	
	#配置环境变量
	if [[ $export = 1 ]];then
		source $bin/$tool/bin.sh $version
		#赋予执行权限
		sudo chmod +x $system_bin/$tool$version.sh
	elif [[ $export = 2 ]];then
		source $bin/$tool/export.sh $tool $version
	else
		echo "没有这个选项。"
		exit
	fi
	
	#如果是python生成配置文件
	if [[ $tool = 'python' ]];then
		source $system_bin/export.sh
		source python$version.sh
		mkdir -p $system_conf/python$version
		
		cp $python_config/pip.template $system_conf/python$version/pip.conf
		echo "#缓存" >> $system_conf/python$version/pip.conf
		echo "cache-dir = $path/system_cache/python$version/pip" >> $system_conf/python$version/pip.conf
		
		cp $python_config/poetry.template $system_conf/python$version/config.toml
		echo "#缓存" >> $system_conf/python$version/config.toml
		echo "cache-dir = $path/system_cache/python$version/poetry" >> $system_conf/python$version/config.toml
		pip install pipx
		pipx install poetry --force #避免污染python环境
	fi
	
	#如果是nodejs生成配置文件
	if [[ $tool = 'node' ]];then
		source $system_bin/export.sh
		source node$version.sh
		
		#npm config --global set userconfig $path/system_conf/node$version/.npmrc
		npm config --global set cache $path/system_cache/node$version/npm
		npm config --global set registry https://registry.npmmirror.com #换源
		
		npm install pnpm -g

		#pnpm config --global set userconfig $path/system_conf/node$version/.pnpmrc
		pnpm config --global set store-dir $path/system_cache/node$version/pnpm
		pnpm config -g set cache-dir $path/system_cache/node$version/pnpm
		pnpm config --global set registry https://registry.npmmirror.com
		npm install cnpm -g #必须全局安装
	fi
fi

